#include "LifeChecker.h"
#include <iostream>

LifeChecker::LifeChecker(int xValue, int yValue)
{
    gameBoard = new LifeBoard(xValue, yValue);
    curX = 0;
    curY = 0;
}

LifeChecker::~LifeChecker()
{
    delete gameBoard;
    //sets the memory space back to a null value
    gameBoard = 0;
}

//sets the xValue that will be checked in the LifeChecker Class
void LifeChecker::setCurX(int xValue){
  curX = xValue;
}

//sets the yValue that will be checked in the LifeChecker Class
void LifeChecker::setCurY(int yValue){
  curY = yValue;
}

//returns the x value of the current space being checked
int LifeChecker::getCurX(){
  return curX;
}

//returns the y value of the current space being checked
int LifeChecker::getCurY(){
  return curY;
}

LifeBoard LifeChecker::nextGeneration(LifeBoard * gameCurrState){
    gameBoard->copy(gameCurrState);

    for(int x = 0; x < gameBoard->getLifeBoardXRange(); x++){
      for(int y = 0; y < gameBoard->getLifeBoardYRange(); y++){
        gameBoard->setLifeBoard(isAlive(), x, y);
      }
    }
    return *gameBoard;
}
//Will run all of the other checks and will determine based upon how
//many return true if the cell will be alive next generation
//0 - 1 = 0 Lonely
//4 - 8 = 0 Overcrowded
//2 - 3 = 1 Lives
//3     = 1 Birth
bool LifeChecker::isAlive(){
    int count = 0;

    if(lifeUpChecker()){count++;}
    if(lifeDownChecker()){count++;}
    if(lifeRightChecker()){count++;}
    if(lifeLeftChecker()){count++;}
    if(lifeUpRightChecker()){count++;}
    if(lifeUpLeftChecker()){count++;}
    if(lifeDownRightChecker()){count++;}
    if(lifeDownLeftChecker()){count++;}

    if(count > 3 || count < 2 || (count == 2 && (gameBoard->getLifeBoardValue(curX, curY) == 0))){
      return false;
    }

    return true;
}

//checks if the cell above is alive
bool LifeChecker::lifeUpChecker(){
  if(curY > 0){
    if(gameBoard->getLifeBoardValue(curX, curY - 1) == 1){
      return true;
    }
  }

  return false;
}

//checks if the cell below is alive
bool LifeChecker::lifeDownChecker(){
  if(curY < (gameBoard->getLifeBoardYRange() - 1)){
    if(gameBoard->getLifeBoardValue(curX, curY + 1) == 1){
      return true;
    }
  }

  return false;
}

//checks if the cell to the right is alive
bool LifeChecker::lifeRightChecker(){
  if(curX < (gameBoard->getLifeBoardXRange() - 1)){
    if(gameBoard->getLifeBoardValue(curX + 1, curY) == 1){
      return true;
    }
  }

  return false;
}

//checks if the cell to the left is alive
bool LifeChecker::lifeLeftChecker(){
  if(curX > 0){
    if(gameBoard->getLifeBoardValue(curX - 1, curY) == 1){
      return true;
    }
  }

  return false;
}

//checks if the cell to the right of the above is alive
bool LifeChecker::lifeUpRightChecker(){
  if(curX < (gameBoard->getLifeBoardXRange() - 1)){
    if(curY > 0){
      if(gameBoard->getLifeBoardValue(curX + 1, curY - 1) == 1){
        return true;
      }
    }
  }

  return false;
}

//checks if the cell to the left of the above is alive
bool LifeChecker::lifeUpLeftChecker(){
  if(curX > 0){
    if(curY > 0){
      if(gameBoard->getLifeBoardValue(curX - 1, curY - 1) == 1){
        return true;
      }
    }
  }

  return false;
}

//checks if the cell to the right of the below is alive
bool LifeChecker::lifeDownRightChecker(){
  if(curX < (gameBoard->getLifeBoardXRange() - 1)){
    if(curY < (gameBoard->getLifeBoardYRange() - 1)){
      if(gameBoard->getLifeBoardValue(curX + 1, curY + 1)){
        return true;
      }
    }
  }

  return false;
}

//checks if the cell to the left of the below is alive
bool LifeChecker::lifeDownLeftChecker(){
  if(curX > 0){
    if(curY < (gameBoard->getLifeBoardYRange() - 1)){
      if(gameBoard->getLifeBoardValue(curX - 1, curY + 1)){
        return true;
      }
    }
  }

  return false;
}
